﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using GHI.Premium.System;
using GHI.Premium.USBHost;

namespace GadgeteerEnOcean
{
    public partial class Program
    {
        Module.DisplayModule.SimpleGraphicsInterface sg;
        uint height;
        uint width;
        uint y;
        uint thickness;
        uint offset;

        uint potiX;
        uint potiY;
        uint tempX;
        uint tempY;

        USBH_SerialUSB serialPort;
        Thread serialPortThread;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            display_T35.SimpleGraphics.AutoRedraw = true;

            //display_T35.WPFWindow.TouchDown += new Microsoft.SPOT.Input.TouchEventHandler(WPFWindow_TouchDown);
            //display_T35.WPFWindow.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(WPFWindow_TouchUp);

            display_T35.WPFWindow.Invalidate();

            Thread startThread = new Thread(StartDisplay);

            startThread.Start();

            // Subscribe to USBH event.
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        void StartDisplay()
        {
            Thread.Sleep(500);

            height = display_T35.Height / 4;
            width = display_T35.Width / 2;
            y = height * 3;
            thickness = 2;
            offset = thickness / 2;

            potiX = tempX = display_T35.Width / 2;
            tempY = display_T35.Height / 4 - 20;
            potiY = display_T35.Height / 2 - 20;

            sg = display_T35.SimpleGraphics;

            sg.Clear();
            sg.DisplayRectangle(Colors.Green, thickness, Colors.Black, 0, y, width - offset, height);
            sg.DisplayRectangle(Colors.Red, thickness, Colors.Black, width + offset, y, width - offset, height);

            DisplayTemperature("TEMP");
            if (co2Sensor)
            {
                DisplayCO2("CO2");
            }
            else if (displayHumid)
            {
                DisplayHumidity("HUMID");
            }
        }

        object DisplayTemperature(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.Msyh24),
                //    Colors.Yellow, tempX - 5, tempX);

                sg.DisplayRectangle(Colors.Orange, 1, Colors.Black, tempX - 100, tempY, 200, 50);

                sg.DisplayTextInRectangle(text, tempX - 100, tempY,
                    200, //width
                    50, //height
                    Colors.Yellow,
                    Resources.GetFont(Resources.FontResources.Msyh24),
                    Module.DisplayModule.SimpleGraphicsInterface.TextAlign.Center,
                    Module.DisplayModule.SimpleGraphicsInterface.WordWrap.Wrap,
                    Module.DisplayModule.SimpleGraphicsInterface.Trimming.WordEllipsis,
                    Module.DisplayModule.SimpleGraphicsInterface.ScaleText.ScaleToFit
                );
            }
            return null;
        }

        object DisplayHumidity(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.Msyh24),
                //    Colors.Cyan, potiX, potiY);

                sg.DisplayRectangle(Colors.Magenta, 1, Colors.Black, potiX - 100, potiY, 200, 50);

                sg.DisplayTextInRectangle(text, potiX - 100, potiY,
                    200, //width
                    50, //height
                    Colors.Cyan,
                    Resources.GetFont(Resources.FontResources.Msyh24),
                    Module.DisplayModule.SimpleGraphicsInterface.TextAlign.Center,
                    Module.DisplayModule.SimpleGraphicsInterface.WordWrap.Wrap,
                    Module.DisplayModule.SimpleGraphicsInterface.Trimming.WordEllipsis,
                    Module.DisplayModule.SimpleGraphicsInterface.ScaleText.ScaleToFit
                    );
            }
            return null;
        }

        object DisplayCO2(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.NinaB),
                //sg.DisplayText(text, Resources.GetFont(Resources.FontResources.Msyh24),
                //    Colors.Cyan, potiX, potiY);

                sg.DisplayRectangle(Colors.LightGray, 1, Colors.Black, potiX - 100, potiY, 200, 50);

                sg.DisplayTextInRectangle(text, potiX - 100, potiY,
                    200, //width
                    50, //height
                    Colors.Cyan,
                    Resources.GetFont(Resources.FontResources.Msyh24),
                    Module.DisplayModule.SimpleGraphicsInterface.TextAlign.Center,
                    Module.DisplayModule.SimpleGraphicsInterface.WordWrap.Wrap,
                    Module.DisplayModule.SimpleGraphicsInterface.Trimming.WordEllipsis,
                    Module.DisplayModule.SimpleGraphicsInterface.ScaleText.ScaleToFit
                    );
            }
            return null;
        }

        object DisplayDebug(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                sg.DisplayRectangle(Colors.Black, 1, Colors.Black, 5, 5, 100, 26);

                sg.DisplayText(text, Resources.GetFont(Resources.FontResources.Msyh24),
                    Colors.White, 5, 5);
            }
            return null;
        }

        object labelEEPText(object obj)
        {
            string text = obj as string;
            if (text != null)
            {
                sg.DisplayRectangle(Colors.Black, 1, Colors.Black, 192, 5, 128, 26);

                sg.DisplayText(text, Resources.GetFont(Resources.FontResources.Msyh24),
                    Colors.Blue, 192, 5);
            }
            return null;
        }

        void DeviceConnectedEvent(USBH_Device device)
        {
            Debug.Print("Device connected");

            switch (device.TYPE)
            {
                case USBH_DeviceType.Serial_FTDI: // FTDI connected
                    serialPort = new USBH_SerialUSB(device, 57600,
                        System.IO.Ports.Parity.None, 8,
                        System.IO.Ports.StopBits.One);
                    serialPort.Open();
                    serialPortThread = new Thread(SerialPortThread);
                    serialPortThread.Start();

                    break;

                default:
                    Debug.Print("Unknown device type = " + device.TYPE);
                    break;
            }
        }

        void SerialUSBThread()
        {
            // Print "Hello World!" every second.
            //byte[] data = Encoding.UTF8.GetBytes("Hello World!\r\n");
            byte[] data = new byte[64];
            int count;

            while (true)
            {
                Thread.Sleep(1000);

                //serialUSB.Write(data, 0, data.Length);
                count = serialPort.Read(data, 0, 64);
                Debug.Print("count = " + count);
            }
        }
    }
}
